﻿using KeYan.Service.Common.Enum;
using KeYan.Service.IRepository.Shop;
using KeYan.Service.IService.Shop;
using KeYan.Service.Model.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KeYan.Service.Repository.Shop
{
    public class ServeRepository : ServeIRepository
    {
        KeYanContext context = new KeYanContext();
        public async Task<Resilt<int>> AddShopping(int serveid, ServeInfo serveInfo)
        {
            var list = context.tb_user_service_vaild.Where(s => s.ServeId.Equals(serveid)).FirstOrDefault();
            var show = context.tb_user_service_orders.ToList();
            if (context.tb_user_service_serve.Where(s => s.ServeName.Equals(serveid)).Count()>0)
            {
                if (list.StartTime<list.EndTime)
                {
                    list.EndTime = list.EndTime.AddYears(1);
                    return new Resilt<int> { Data = 1, Msg = "购买成功", State = ResultStatus.Success };
                }
            }
            else
            {
                var end = show.Where(s => s.ServeId.Equals(list.ServeId)).FirstOrDefault();
                list.StartTime = end.PayMentTime;
                list.EndTime = list.StartTime.AddYears(1);
                return new Resilt<int> { Data = 1, Msg = "购买成功", State = ResultStatus.Success };

            }
            return new Resilt<int> { Data = 0, Msg = "购买失败", State = ResultStatus.Error }; 
        }
    }
}
